float touchMoveSpeedX = 0.5;
float touchMoveSpeedY = 0.1;

class TouchGen
{
  private class Touch
  {
    PVector  pos;
    float    msRemaining;
    PVector  moveVec;
    
    Touch(PVector _pos, float _msRemaining, PVector _moveVec)
    {
      pos = _pos;
      msRemaining = _msRemaining;
      moveVec     = _moveVec;
    }
  }
  
  Vector  touches;
  int     maxSimulTouches;
  float   nextNewTouchTime;
  float   avgNewTouchTime;
  float   avgTouchDuration;
  
  TouchGen(int _maxSimulTouches, float _avgNewTouchTime, float _avgTouchDuration)
  {
    touches           = new Vector();
    maxSimulTouches   = _maxSimulTouches;
    avgNewTouchTime   = _avgNewTouchTime; 
    avgTouchDuration  = _avgTouchDuration;
    addNewTouch();
  }
  
  void update(float milliseconds)
  {
    nextNewTouchTime -= milliseconds;
    if(nextNewTouchTime < 0.0 && touches.size() < maxSimulTouches)
    {
      addNewTouch();
    }
    
    for(int i = 0; i < touches.size(); i++)
    {
      Touch touch = (Touch)touches.elementAt(i);
      touch.msRemaining -= milliseconds;
      
      if(touch.msRemaining <= 0.0)
      {
        // touch expired, remove it
        touches.remove(i);
        i--;
      }
      else
      {
        touch.pos.x += touch.moveVec.x * milliseconds;
        touch.pos.y += touch.moveVec.y * milliseconds;
      }
    }
  }
  
  Vector getTouches()
  {
    Vector points  = new Vector();
    
    for(int i = 0; i < touches.size(); i++)
    {
      points.add(((Touch)touches.elementAt(i)).pos);
    }
    
    return points;
  }
  
  private void addNewTouch()
  {
    float newRemaining  = random(avgTouchDuration*0.5,avgTouchDuration*1.5);
    PVector pos         = new PVector(random(width*0.1,width*0.9),random(height*0.4,height*0.9));
    PVector moveVec;
    if(random(1.0) > 0.5)
      moveVec = new PVector(random(-touchMoveSpeedX,touchMoveSpeedX),random(-touchMoveSpeedY,touchMoveSpeedX));
    else
      moveVec = new PVector(0,0);
    
    Touch touch = new Touch(pos,newRemaining,moveVec);
    
    touches.add(touch);
  
    nextNewTouchTime  = random(avgNewTouchTime*0.5,avgNewTouchTime*1.5);
  }
}


